Skip to content

Comments

Introduce a #[diagnostic::on_unknown_item] attribute#152901

Open
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:feature/on_unknown_item
Open

Introduce a #[diagnostic::on_unknown_item] attribute#152901
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:feature/on_unknown_item

Conversation

@weiznich
Copy link
Contributor

@weiznich weiznich commented Feb 20, 2026

This PR introduces a #[diagnostic::on_unknown_item] attribute that allows crate authors to customize the error messages emitted by unresolved imports. The main usecase for this is using this attribute as part of a proc macro that expects a certain external module structure to exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel, that expect to refer to a tabe module. That is done either implicitly (via the name of the type with the derive) or explicitly by the user. This attribute would allow us to improve the error message in both cases:

  • For the implicit case we could explicity call out our assumptions (turning the name into lower case, adding an s in the end)
  • point to the explicit variant as alternative
  • For the explicit variant we would add additional notes to tell the user why this is happening and what they should look for to fix the problem (be more explicit about certain diesel specific assumptions of the module structure)

I assume that similar use-cases exist for other proc-macros as well, therefore I decided to put in the work implementing this new attribute. I would also assume that this is likely not useful for std-lib internal usage.

related #152900 and #128674

@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 20, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 68 candidates
  • Random selection from 15 candidates

@rust-log-analyzer

This comment has been minimized.

@weiznich weiznich force-pushed the feature/on_unknown_item branch from 5c63f6f to a9dd689 Compare February 20, 2026 13:33
@rust-log-analyzer

This comment has been minimized.

This PR introduces a `#[diagnostic::on_unknown_item]` attribute that
allows crate authors to customize the error messages emitted by
unresolved imports. The main usecase for this is using this attribute as
part of a proc macro that expects a certain external module structure to
exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel,
that expect to refer to a `tabe` module. That is done either
implicitly (via the name of the type with the derive) or explicitly by
the user. This attribute would allow us to improve the error message in
both cases:

* For the implicit case we could explicity call out our
assumptions (turning the name into lower case, adding an `s` in the end)
+ point to the explicit variant as alternative
* For the explicit variant we would add additional notes to tell the
user why this is happening and what they should look for to fix the
problem (be more explicit about certain diesel specific assumptions of
the module structure)

I assume that similar use-cases exist for other proc-macros as well,
therefore I decided to put in the work implementing this new attribute.
I would also assume that this is likely not useful for std-lib internal
usage.
@weiznich weiznich force-pushed the feature/on_unknown_item branch from a9dd689 to c87fc9e Compare February 20, 2026 14:37
@jdonszelmann
Copy link
Contributor

@weiznich maybe this should be rebased on #151558

@weiznich
Copy link
Contributor Author

I can try this, although I need to read up on how the new infrastructure works and check if it's possible to use this inside of the name resolution stage. Somehow certain things (like lints) act weirdly there.

@jdonszelmann
Copy link
Contributor

The attribute refactor is pretty much finished, which means all old style parsers at this point have been removed from the compiler. There are many examples of new-parsing-infrastructure attribute parsers that work at this stage of the compiler (and none more that work using the old system). I don't think I want to accept any new old-style attribute parsers into the compiler anymore for that reason.

r? me

@rustbot rustbot assigned jdonszelmann and unassigned nnethercote Feb 23, 2026
@weiznich
Copy link
Contributor Author

@jdonszelmann I can totally understand that you don't want to accept any attributes using the old style. Your comment as currently written is still not useful for me as person that contributes only from time to time to the compiler and doesn't keep up with all the internal changes all the time. I get that I need to change something, but it is really unclear for me:

  • What need to change exactly
  • How the new attribute parsing framework works
  • If the new attribute parsing framework would allow me to access the relevant attribute information during name resolution, which as far as I know is kind of a strange place as it is between compiler phases and doesn't have access to the full set of information yet

It's especially not helpful to write that "There are many examples of new-parsing-infrastructure attribute parsers" without even providing a link to one of them.

Do you have any documentation or other hints where to get these information from? Otherwise I fear it's impossible for me to satisfy these requests with the limited amount of time I'm able to spend on this change.

@jdonszelmann
Copy link
Contributor

Fair enough, take a look at how we handle MacroUse in rustc_resolve (grep for it). It should look similar to that. For that attribute we similarly have less information available, but it works using parse_limited.

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 24, 2026

☔ The latest upstream changes (presumably #153047) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#151558 has merged now so you can rebase on that.

What need to change exactly

I have some review comments as well. Thanks for continuing this work by the way :)

View changes since this review

Comment on lines 703 to +711
const DIAG_ATTRS: &[Symbol] =
&[sym::on_unimplemented, sym::do_not_recommend, sym::on_const];
&[sym::on_unimplemented, sym::do_not_recommend, sym::on_const, sym::on_unknown_item];

if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)
&& let [namespace, attribute, ..] = &*path.segments
&& namespace.ident.name == sym::diagnostic
&& !DIAG_ATTRS.contains(&attribute.ident.name)
&& (!DIAG_ATTRS.contains(&attribute.ident.name)
|| (attribute.ident.name == sym::on_unknown_item
&& !self.tcx.features().diagnostic_on_unknown_item()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to handle this in a more general way since on_const and (soon) on_move are also unstable. Such an expression is going to be unwieldy if we do this for those as well.

something like let diag_attrs = /* vec of all accessible diagnostic attrs */

Comment on lines +1 to +17
#![feature(diagnostic_on_unknown_item)]
pub mod foo {
pub struct Bar;
}

#[diagnostic::on_unknown_item(
message = "first message",
label = "first label",
note = "custom note",
note = "custom note 2"
)]
use foo::Foo;
//~^ERROR first message

use foo::Bar;

fn main() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to support extern crate declarations? If so please add a test like

#[diagnostic::on_unknown_item(
    message = "first message",
    label = "first label",
    note = "custom note",
    note = "custom note 2"
)]
extern crate foo;

Or if you don't, add it to incorrect-locations.rs

//~^ ERROR: custom message
};
}
fn main() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for use inside the use declaration, like

mod test5 {
    use std::{
        string::String,
        #[diagnostic::on_unknown_item(
           message = "custom message",
           label = "custom label",
            note = "custom note"
        )]
        vec::{NonExisting, Vec},
        //~^ ERROR: custom message
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants